package com.vita.core;

import com.vita.common.FileUtils;
import com.vita.core.function.Func;
import com.vita.core.device.output.TxtOutput;

import java.util.Objects;

public class Helper {
    
    public static <I> Ico input(I i){
        Objects.requireNonNull(i);
        Ico b = new Ico();
        Func input;
        if (i instanceof Func){
            input = (Func) i;
        }else{
            b.setValue(i);
            input = (o)->o;
        }
        return b.input(input);
    }

    public static Ico convert(Func c){
        Objects.requireNonNull(c);
        Ico ico = new Ico();
        ico.convert(c);
        return ico;
    }

    public static <O> Func output(O o){
        Func output = null;
        if (o instanceof Func){
            output = (Func) o;
        }else if(o instanceof String){
            String s = (String) o ;
            if (FileUtils.isTxtFile(s)){
                output = new TxtOutput();
            }
        }else{
            output = (ou)->ou;
        }
        return output;
    }

    /**
     * Ico复制，复制过后的Ico会继承旧Ico的债务
     * 一次性（已）还款债务不需要再次还，分期还款需要接着还
     * @param oldIco
     * @param newIco
     */
    public static void IcoCopy(Ico oldIco,Ico newIco){
        oldIco.run();
        newIco.input(oldIco.getInputAct())
              .convert(oldIco.getConvertAct())
              .output(oldIco.getOutputAct());
    }
}
